package com.cinema.entity;

import java.time.LocalDateTime;

/**
 * 排片实体类
 */
public class Schedule {
    private String id;
    private Movie movie;
    private ScreeningRoom room;
    private LocalDateTime startTime;
    private LocalDateTime endTime;
    private String[][] seatStatus; // 座位状态矩阵，null表示空位，非null表示已售出（存储手机号）
    
    /**
     * 构造方法
     * @param id 排片ID
     * @param movie 电影
     * @param room 放映厅
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    public Schedule(String id, Movie movie, ScreeningRoom room, LocalDateTime startTime, LocalDateTime endTime) {
        this.id = id;
        this.movie = movie;
        this.room = room;
        this.startTime = startTime;
        this.endTime = endTime;
        // 初始化座位状态矩阵
        initializeSeatStatus();
    }
    
    /**
     * 初始化座位状态矩阵
     */
    private void initializeSeatStatus() {
        this.seatStatus = new String[room.getRows()][room.getCols()];
        // 所有座位初始化为null（空位）
        for (int i = 0; i < room.getRows(); i++) {
            for (int j = 0; j < room.getCols(); j++) {
                seatStatus[i][j] = null;
            }
        }
    }
    
    // Getter和Setter方法
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public Movie getMovie() {
        return movie;
    }
    
    public void setMovie(Movie movie) {
        this.movie = movie;
    }
    
    public ScreeningRoom getRoom() {
        return room;
    }
    
    public void setRoom(ScreeningRoom room) {
        this.room = room;
    }
    
    public LocalDateTime getStartTime() {
        return startTime;
    }
    
    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }
    
    public LocalDateTime getEndTime() {
        return endTime;
    }
    
    public void setEndTime(LocalDateTime endTime) {
        this.endTime = endTime;
    }
    
    public String[][] getSeatStatus() {
        return seatStatus;
    }
    
    public void setSeatStatus(String[][] seatStatus) {
        this.seatStatus = seatStatus;
    }
    
    /**
     * 检查座位是否可用
     * @param row 行号（从0开始）
     * @param col 列号（从0开始）
     * @return true表示可用，false表示已售出
     */
    public boolean isSeatAvailable(int row, int col) {
        if (row < 0 || row >= room.getRows() || col < 0 || col >= room.getCols()) {
            return false; // 座位不存在
        }
        return seatStatus[row][col] == null;
    }
    
    /**
     * 售出座位
     * @param row 行号
     * @param col 列号
     * @param phone 手机号
     * @return true表示成功，false表示失败
     */
    public boolean sellSeat(int row, int col, String phone) {
        if (row < 0 || row >= room.getRows() || col < 0 || col >= room.getCols()) {
            return false; // 座位不存在
        }
        if (seatStatus[row][col] != null) {
            return false; // 座位已售出
        }
        seatStatus[row][col] = phone;
        return true;
    }
    
    /**
     * 获取已售座位数
     * @return 已售座位数
     */
    public int getSoldSeatsCount() {
        int count = 0;
        for (int i = 0; i < room.getRows(); i++) {
            for (int j = 0; j < room.getCols(); j++) {
                if (seatStatus[i][j] != null) {
                    count++;
                }
            }
        }
        return count;
    }
    
    /**
     * 获取剩余座位数
     * @return 剩余座位数
     */
    public int getAvailableSeatsCount() {
        return room.getTotalSeats() - getSoldSeatsCount();
    }
    
    @Override
    public String toString() {
        return "Schedule{" +
                "id='" + id + '\'' +
                ", movie=" + movie.getName() +
                ", room=" + room.getName() +
                ", startTime=" + startTime +
                ", endTime=" + endTime +
                ", soldSeats=" + getSoldSeatsCount() +
                ", availableSeats=" + getAvailableSeatsCount() +
                '}';
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Schedule schedule = (Schedule) obj;
        return id != null ? id.equals(schedule.id) : schedule.id == null;
    }
    
    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
} 